import { loginAPI } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
const state = () => {
  return {
    token: getToken() || ''
  }
}
// 修改state里面的数据
const mutations = {
  updateToken(state, token) {
    state.token = token
    // 将token存储到cookie中
    setToken(token)
  },
  deleteToken(state) {
    state.token = ''
    // 将token清空
    removeToken()
  }
}

// 发送异步请求
const actions = {
  async doLogin(cxt, data) {
    const res = await loginAPI(data)
    console.log(res)
    cxt.commit('updateToken', res.data.token)
    setToken(res.data.token)
  }
}

export default {
  // 开启命名空间
  namespaced: true,
  state,
  mutations,
  actions
}
